package com.hhs.FileUpload.controller;


import com.hhs.FileUpload.util.IpAddr;
import com.hhs.FileUpload.util.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("upload")
public class UploadController {
    // 获取文件上传的位置
    @Value("${spring.servlet.multipart.location}")
    private String uploadPath;

    @Value("${server.port}")
    private String port;

    // 获取虚拟路径
    @Value("${virtualFileDepositPath}")
    private String virtualFileDepositPath;
    @RequestMapping("multipartFile")
    public Result upload(MultipartFile multipartFile){
        String originalFilename=multipartFile.getOriginalFilename();
        File file=new File(uploadPath,originalFilename);
        try {
            multipartFile.transferTo(file);
        }catch (IOException e){
            throw new RuntimeException(e);
        }
        // 获取当前的ip地址
        String serverIp = IpAddr.getServerIp();
        // 返回的地址

        String filePath= "http://"+serverIp+":"+port+"/"+virtualFileDepositPath;
        return Result.success("成功");
    }
}
